<template>
  <div>
    <van-nav-bar title="新增/编辑收货地址" left-text="返回" left-arrow @click-left="$router.back()" />
    <div class="addressnewList ">
      <van-address-edit :address-info="addr" :area-list="$store.areaData" show-delete @save="onSave"
        @delete="onDelete" />
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {

      }
    },
    computed: {
      addr() {
        if (this.$store.user.id) {
          for (let i = 0; i < this.$store.user.addressList.length; i++) {
            if (this.$store.user.addressList[i].id == this.$route.params.id) {
              return this.$store.user.addressList[i]
            }
          }
        }
      }
    },
    methods: {
      onSave(content) {
        content = this.combineAddress(content, true)
        if (this.$route.params.id > 0) {
          this.$axios.post(`/user_address/update?id=${this.$route.params.id}`, content).then(res => {
            if (res.errcode == 0) {
              this.$Toast('修改地址成功!')
              for (let i = 0; i < this.$store.user.addressList.length; i++) {
                if (this.$store.user.addressList[i].id == this.$route.params.id) {
                  this.$store.user.addressList[i] = content
                }
              }
              this.$router.back()
            }
          })
        } else {
          this.$axios.post('/user_address/create', content).then(res => {
            if (res.errcode == 0) {
              this.$Toast('添加地址成功!')
              this.$store.user.addressList.unshift(res.data)
              this.$router.back()
            }
          })
        }

      },
      onDelete() {
        
      }
    },
    mounted() {
      this.$axios.get('/data/area').then(res => {
        this.$store.area = res.data
        if (res.errorcode == 0) {
        }
      })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  .addressnewList {
    height: calc(100vh - 46px);
    -webkit-overflow-scrolling: touch;
    overflow-y: scroll;
  }
</style>